package com.itheima.mongo.api;

import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.UserLocationApi;
import com.itheima.model.domain.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {
        try {
            //根据用户id构造查询条件
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            //判断是否为空
            if (location == null) {
                //为空保存用户地址信息
                location = new UserLocation();
                location.setLocation(new GeoJsonPoint(longitude, latitude));
                location.setCreated(System.currentTimeMillis());
                location.setUserId(userId);
                location.setAddress(address);
                location.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(location);
            } else {
                //存在就更新
                Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", location.getLastUpdated());
                mongoTemplate.updateFirst(query, update, UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Long> search(Long id, Double value) {
        //根据用户id查询
        Criteria criteria = Criteria.where("userId").is(id);
        Query query = Query.query(criteria);
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        //判断是否为bull
        if (location == null) {
            return null;
        }
        //以当前用户为圆心
        GeoJsonPoint point = location.getLocation();
        //距离为半径
        Distance distance = new Distance(value / 1000, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle = new Circle(point, distance);
        //查询
        Query localtionQuery = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> list = mongoTemplate.find(localtionQuery, UserLocation.class);
        //获取用户id
        List<Long> longs = CollUtil.getFieldValues(list, "userId", Long.class);
        return longs;
    }
}
